package com.xiaohai.big_event_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaohai.big_event_system.mapper.ArticleMapper;
import com.xiaohai.big_event_system.projo.Article;
import com.xiaohai.big_event_system.service.IArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * <p>
 * 文章 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-01-15
 */
@Slf4j
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public IPage<Article> list(Integer currentPage, Integer pageSize, Article article) {
        Page<Article> page = new Page<>(currentPage, pageSize);
        log.info("currentPage: {}, pageSize: {}, article: {}", currentPage, pageSize, article);
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        Optional.ofNullable(article.getTitle()).ifPresent(title -> queryWrapper.like(Article::getTitle, title));
        Optional.ofNullable(article.getCreateUser()).ifPresent(user -> queryWrapper.like(Article::getCreateUser, user));
        Optional.ofNullable(article.getCategoryId()).ifPresent(categoryId -> queryWrapper.eq(Article::getCategoryId, categoryId));
        Optional.ofNullable(article.getState()).ifPresent(state -> queryWrapper.like(Article::getState, state));
        return this.page(page, queryWrapper);
    }
}
